//
//  ChooseViewController.m
//  MyMovie
//
//  Created by Weiwei on 16/2/24.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "ChooseViewController.h"
#import "TopView.h"
#import "CommentView.h"
#import "NewsView.h"
#import "NoticeView.h"
#import "NewsModel.h"
#import "ChartsModel.h"
#import "NoticeModel.h"
@interface ChooseViewController (){
    
    TopView* _topView;
    CommentView* _commentView;
    NewsView* _newView;
    NoticeView* _noticeView;
    
    //----------------
    NSMutableArray* _newsViewDataList;
    NSMutableArray* _noticeViewDataList;
    NSMutableArray* _topViewDataList;
    NSMutableArray* _commentViewDataList;
    
    
}

@end

@implementation ChooseViewController


//重写init方法，在viewC被创建的时候就给他的navigationItem和tabBarItem
-(id)init{
    if (self = [super init]) {
        
        
        self.navigationItem.title = @"精选";
        self.tabBarItem.image = [UIImage imageNamed:@"index_menu_top"];
        
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
#pragma -mark 增加一个解析数据的方法
    [self loadData];
    
    
    
#pragma -mark 目标一创建一个segment放到导航栏上
    [self createSegment];
    
#pragma -mark 目标二 创建出四个view，每个view搭载一个tableView
    [self createSubViews];
}



-(void)loadData{
    
    
     _newsViewDataList = [[NSMutableArray alloc]initWithCapacity:1];
     _noticeViewDataList = [[NSMutableArray alloc]initWithCapacity:1];
     _topViewDataList = [[NSMutableArray alloc ]initWithCapacity:1];
     _commentViewDataList = [[NSMutableArray alloc]initWithCapacity:1];
    
    
    
    //--------------new---------------
    NSDictionary* newsDataDic = [DataParser jsonObjectFromFileName:@"find_news"];
    NSArray* newsList = [newsDataDic objectForKey:@"newsList" ];
    
    for (NSDictionary* dic in newsList) {
        
        NewsModel* model = [[NewsModel alloc]initWithDic:dic];
        //创建四个dataList数组，用于存放不同view的数据
        
        [_newsViewDataList addObject:model];
        
    }
    
    //数据创建好，要给view增加一个dataList属性，用于接受VC给他的数据
    
    //---------------TOP -----------------
    
    //排行数据
    NSDictionary *charts = [DataParser jsonObjectFromFileName:@"rank2"];
    
    NSArray *topLists = [charts objectForKey:@"topLists"];
    
    for (NSDictionary *dic in topLists) {
        
        ChartsModel *model = [[ChartsModel alloc] initWithDic:dic];
        [_topViewDataList addObject:model];
        
    }
  
    
   //----------- notice ----------------
    
    //1.获取根字典
    NSDictionary* noticeData = [DataParser jsonObjectFromFileName:@"预告"];
    NSArray* trailers = [noticeData objectForKey:@"trailers"];
    
    for (NSDictionary* dic in trailers) {
        
        NoticeModel* model = [[NoticeModel alloc]initWithDic:dic];
        [_noticeViewDataList addObject:model];
        

    }
    
    
    
    
}








-(void)createSegment{
    
    SegmentView* _segmentView= [[SegmentView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 44) withTitleArray:@[@"新闻",@"预告片",@"排行榜",@"影评"]];
    //2.将一个view增加到导航栏上
    self.navigationItem.titleView = _segmentView;
    
    
    __weak ChooseViewController* weakSelf = self;
    //3.实现点击
    [_segmentView setSegmentBlock:^(NSInteger index) {
        
        __strong ChooseViewController* strongSelf = weakSelf;
        
        [strongSelf bringViewToFront:index];
        
        
    }];
    
}


-(void)bringViewToFront:(NSInteger)index{
    
    if (index == 0) {
        //显示新闻
        [self.view bringSubviewToFront:_newView];
        
    }else if (index == 1){
        //预告片
        [self.view bringSubviewToFront:_noticeView];
        
        
    }else if (index == 2){
        
        //排行榜
        [self.view bringSubviewToFront:_topView];
        
    }else if(index == 3){
        
        //影评
        [self.view bringSubviewToFront:_commentView];
        
    }
    
    
    
}








-(void)createSubViews{
    
    _topView = [[TopView alloc]initWithFrame:self.view.bounds];
//    _topView.backgroundColor = [UIColor redColor];
    _topView.dataList = _topViewDataList;
    [self.view addSubview:_topView];


   _commentView = [[CommentView alloc]initWithFrame:self.view.bounds];
    _commentView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_commentView];


   _noticeView = [[NoticeView alloc]initWithFrame:self.view.bounds];
//    _noticeView.backgroundColor = [UIColor orangeColor];
    _noticeView.dataList = _noticeViewDataList;
    [self.view addSubview:_noticeView];


   _newView = [[NewsView alloc]initWithFrame:self.view.bounds];
  //  _newView.backgroundColor = [UIColor yellowColor];
#pragma -mark 给view数据
    _newView.dataList = _newsViewDataList;
    
    [self.view addSubview:_newView];
    
 
    
    
    
    
    
    
}
@end
